using System;
using I2.Loc;
using UnityEngine;

public class X3Timer : MonoBehaviour
{
	public GameObject bankX3;

	public GameObject bankNormal;

	private float _lastUpdateTime;

	public UILabel eventX3RemainTime;

	private string _endsInLabel = "ends in";

	private string day = "Day";

	private string days = "Days";

	private long standartTime = 86400L;

	private bool getServerTime;

	private long remainedTotalSeconds;

	private void Start()
	{
		eventX3RemainTime.text = string.Empty;
		day = ScriptLocalization.Get("Lobby_X3/day");
		days = ScriptLocalization.Get("Lobby_X3/days");
		_endsInLabel = ScriptLocalization.Get("Lobby_X3/ends");
		bankX3.SetActive(false);
		bankNormal.SetActive(true);
	}

	private void Update()
	{
		if (!(EventX3Manager.Instance != null))
		{
			return;
		}
		if (EventX3Manager.Instance.IsEventX3Active)
		{
			if (!(Time.realtimeSinceStartup - _lastUpdateTime >= 1f))
			{
				return;
			}
			TimeSpan timeSpan;
			string empty;
			if (JSONManager.Instance.ServerTimeLoad() || getServerTime)
			{
				if (JSONManager.Instance.ServerTimeLoad())
				{
					remainedTotalSeconds = JSONManager.Instance.GetServerTime();
				}
				else
				{
					remainedTotalSeconds--;
				}
				timeSpan = TimeSpan.FromSeconds(remainedTotalSeconds);
				empty = string.Empty;
				long value = timeSpan.Hours * 3600 + timeSpan.Minutes * 60 + timeSpan.Seconds - 86400;
				timeSpan = TimeSpan.FromSeconds(Math.Abs(value));
				getServerTime = true;
			}
			else
			{
				standartTime--;
				timeSpan = TimeSpan.FromSeconds(standartTime);
			}
			empty = string.Format("{0} {1:00}:{2:00}:{3:00}", _endsInLabel, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
			eventX3RemainTime.text = empty;
			_lastUpdateTime = Time.realtimeSinceStartup;
			bankX3.SetActive(true);
		}
		else if (!string.IsNullOrEmpty(eventX3RemainTime.text))
		{
			eventX3RemainTime.text = string.Empty;
			bankX3.SetActive(false);
			bankNormal.SetActive(true);
		}
	}
}
